Page({
  data: {
    menuList: ['图书期刊', '文化用品', '音像内容'],
    currentMenu: 0,
    subData: [
      {
        title: '图书期刊',
        children: [
          { name: '绘本', img: '/pages/book-category/book1.png' },
          { name: '小说', img: '/pages/book-category/book2.png' },
          { name: '漫画', img: '/pages/book-category/book3.png' },
          { name: '教材', img: '/pages/book-category/book4.png' },
          { name: '教辅', img: '/pages/book-category/book5.png' },
          { name: '报纸', img: '/pages/book-category/book6.png' },
          { name: '考公资料', img: '/pages/book-category/book7.png' },
          { name: '英语读物', img: '/pages/book-category/book8.png' },
          { name: '四六级', img: '/pages/book-category/book9.png' },
          { name: '课外读物', img: '/pages/book-category/book10.png' },
          { name: '杂志', img: '/pages/book-category/book11.png' },
          { name: '期刊', img: '/pages/book-category/book12.png' },
          { name: '论文', img: '/pages/book-category/book13.png' },
          { name: '工具书', img: '/pages/book-category/book14.png' },
          { name: '字典', img: '/pages/book-category/book15.png' },
          { name: '百科全书', img: '/pages/book-category/book16.png' },
          { name: '年鉴', img: '/pages/book-category/book17.png' },
          { name: '地图册', img: '/pages/book-category/book18.png' },
          { name: '参考书', img: '/pages/book-category/book19.png' },
          { name: '外文书', img: '/pages/book-category/book20.png' }
        ]
      },
      {
        title: '文化用品',
        children: ['卡册', '显微镜', '笔', '书法用品', '手帐', '墨水瓶', '胶带', '计算器', '笔袋']
      },
      {
        title: '音像内容',
        children: ['专辑', '唱片', '动画片', 'cd', '综艺', '短剧', '电影', '电视剧', '纪录片']
      }
    ]
  },
  onLoad() {
    // 每个大类children分组，每行4个
    const subData = this.data.subData.map(item => {
      let rows = [];
      if (item.title === '图书期刊') {
        // 图书期刊children为对象数组
        for (let i = 0; i < item.children.length; i += 4) {
          rows.push(item.children.slice(i, i + 4));
        }
      } else {
        // 其他为字符串数组
        for (let i = 0; i < item.children.length; i += 4) {
          rows.push(item.children.slice(i, i + 4));
        }
      }
      return { ...item, childrenRows: rows };
    });
    this.setData({ subData });
  },
  onMenuTap(e) {
    const idx = e.currentTarget.dataset.index
    this.setData({
      currentMenu: idx
    })
  },
  onSubTap(e) {
    const name = e.currentTarget.dataset.name
    wx.showToast({ title: name, icon: 'none' })
  },
  goHome() {
    wx.redirectTo({ url: '/pages/index/index' })
  },
  goCategory() {
    wx.redirectTo({ url: '/pages/category/index' })
  }
}) 